package com.jacky.javafx.fx.controller;

import com.jacky.javafx.entities.AppUserViewModel;
import com.jacky.javafx.enums.AppUserStateEnum;
import com.jacky.javafx.enums.GenderEnum;
import com.jacky.javafx.enums.UserGroupEnum;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Paint;
import javafx.scene.text.Font;
import javafx.util.Callback;
import javafx.util.StringConverter;
import org.springframework.stereotype.Component;

import java.net.URL;
import java.util.ResourceBundle;

@Component
public class AppUserDetailController implements Initializable {

    @FXML
    private Button btnEdit;

    @FXML
    private Button btnSave;

    @FXML
    private AnchorPane ap;

    @FXML
    private ComboBox<GenderEnum> cbbGender;
    @FXML
    private ComboBox<AppUserStateEnum> cbbState;

    @FXML
    private ComboBox<UserGroupEnum> cbbUserGroup;

    @FXML
    private TextField txtId;

    @FXML
    private TextField txtLoginName;

    @FXML
    private TextField txtNickName;

    @FXML
    private TextField txtOpenId;

    @FXML
    private TextField txtRegistryTime;

    private AppUserViewModel model;
    private AppUserViewModel backModel;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

    }

    public void model(AppUserViewModel model) {
        this.model = model;
        this.backModel = model;
        initData();
    }

    @FXML
    public void clickEdit(ActionEvent event) {
        txtLoginName.setDisable(false);
        txtOpenId.setDisable(false);
        txtNickName.setDisable(false);
        cbbGender.setDisable(false);
        cbbUserGroup.setDisable(false);
        cbbState.setDisable(false);
        txtRegistryTime.setDisable(false);
        btnEdit.setVisible(false);
        btnSave.setVisible(true);
    }

    private void initData() {
        System.out.println("model:" + model);
        txtId.setText(String.valueOf(model.getId()));
        txtLoginName.setText(String.valueOf(model.getLoginName()));
        txtNickName.setText(String.valueOf(model.getNickName()));
        txtOpenId.setText(String.valueOf(model.getOpenId()));
        txtRegistryTime.setText(String.valueOf(model.getRegistryTime()));
        initGenderComboBox();
        initStateComboBox();
        initUserGroupComboBox();
    }

    private void initUserGroupComboBox() {
        UserGroupEnum userGroupEnum = UserGroupEnum.findByZh(model.getUserGroup());
        cbbUserGroup.getItems().addAll(UserGroupEnum.values());
        cbbUserGroup.setPlaceholder(new Label("请选择用户所属组"));
        cbbUserGroup.setEditable(true);
        cbbUserGroup.setValue(userGroupEnum);
        System.out.println("user group:" + userGroupEnum);
        cbbUserGroup.setConverter(new StringConverter<>() {
            @Override
            public String toString(UserGroupEnum genderEnum) {
                return genderEnum.zh();
            }

            @Override
            public UserGroupEnum fromString(String zh) {
                return UserGroupEnum.findByZh(zh);
            }
        });

        // 自定义item
        cbbUserGroup.setCellFactory(new Callback<>() {
            @Override
            public ListCell<UserGroupEnum> call(ListView<UserGroupEnum> view) {
                return new ListCell<>() {
                    @Override
                    protected void updateItem(UserGroupEnum item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            this.setText(cbbUserGroup.getConverter().toString(item));
                            this.setFont(Font.font(16));
//                            this.setTextFill(Paint.valueOf("#088"));

//                            this.setStyle("-fx-text-fill: #0aa");
//                            this.setGraphic(new ImageView("image\\stash.png"));
                        } else {
                            setText(null);
                            setGraphic(null);
                        }
                    }
                };
            }
        });

        cbbUserGroup.getSelectionModel().selectedItemProperty().addListener((observableValue, stateEnum, t1) -> {

        });
    }

    private void initStateComboBox() {
        AppUserStateEnum stateEnum = AppUserStateEnum.findByZh(model.getState());
        cbbState.getItems().addAll(AppUserStateEnum.values());
        cbbState.setPlaceholder(new Label("请选择用户状态"));
        cbbState.setEditable(true);
        cbbState.setValue(stateEnum);
        System.out.println("user state:" + stateEnum);
        cbbState.setConverter(new StringConverter<>() {
            @Override
            public String toString(AppUserStateEnum genderEnum) {
                return genderEnum.zh();
            }

            @Override
            public AppUserStateEnum fromString(String zh) {
                return AppUserStateEnum.findByZh(zh);
            }
        });

        // 自定义item
        cbbState.setCellFactory(new Callback<ListView<AppUserStateEnum>, ListCell<AppUserStateEnum>>() {
            @Override
            public ListCell<AppUserStateEnum> call(ListView<AppUserStateEnum> view) {
                return new ListCell<AppUserStateEnum>() {
                    @Override
                    protected void updateItem(AppUserStateEnum item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            this.setText(cbbState.getConverter().toString(item));
                            this.setFont(Font.font(16));
//                            this.setTextFill(Paint.valueOf("#088"));

//                            this.setStyle("-fx-text-fill: #0aa");
//                            this.setGraphic(new ImageView("image\\stash.png"));
                        } else {
                            setText(null);
                            setGraphic(null);
                        }
                    }
                };
            }
        });

        cbbState.getSelectionModel().selectedItemProperty().addListener((observableValue, stateEnum1, t1) -> {

        });
    }

    private void initGenderComboBox() {
        GenderEnum gender = GenderEnum.findByZh(model.getGender());
        cbbGender.getItems().addAll(GenderEnum.values());
        cbbGender.setPlaceholder(new Label("请选择性别"));
        cbbGender.setEditable(true);
        cbbGender.setValue(gender);
//        cbbGender.setDisable(false);
        System.out.println("gender:" + gender);
        cbbGender.setConverter(new StringConverter<>() {
            @Override
            public String toString(GenderEnum genderEnum) {
                return genderEnum.zh();
            }

            @Override
            public GenderEnum fromString(String zh) {
                return GenderEnum.findByZh(zh);
            }
        });

        // 自定义item
        cbbGender.setCellFactory(new Callback<ListView<GenderEnum>, ListCell<GenderEnum>>() {
            @Override
            public ListCell<GenderEnum> call(ListView<GenderEnum> view) {
                return new ListCell<GenderEnum>() {
                    @Override
                    protected void updateItem(GenderEnum item, boolean empty) {
                        super.updateItem(item, empty);
                        if (!empty) {
                            this.setText(cbbGender.getConverter().toString(item));
                            this.setFont(Font.font(16));
//                            this.setTextFill(Paint.valueOf("#088"));

//                            this.setStyle("-fx-text-fill: #0aa");
//                            this.setGraphic(new ImageView("image\\stash.png"));
                        } else {
                            setText(null);
                            setGraphic(null);
                        }
                    }
                };
            }
        });

        cbbGender.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<GenderEnum>() {
            @Override
            public void changed(ObservableValue<? extends GenderEnum> oriVal, GenderEnum oldVal, GenderEnum newVal) {
                System.out.println("observableValue: " + oriVal.getValue());
                System.out.println("new value: " + newVal.zh());
                System.out.println("old value: " + oldVal.zh());
            }
        });
    }
}
